package com.eddie.libray;

import com.eddie.libray.entity.BookEntity;
import com.eddie.libray.entity.LoginUser;
import com.eddie.libray.entity.UserEntity;
import lombok.Data;

import java.text.NumberFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 图书馆数据源维护
 *
 * @author Eddie
 * @since 2022/09/02 09:15
 **/
public class LibraryDataSource {

    /**
     * 定义私有构造器，该实例不可创建
     */
    private LibraryDataSource() {
    }

    /**
     * 书本维护
     */
    public static List<BookEntity> books;

    /**
     * 用户数据维护 k->用户名，v->用户详细信息
     */
    public static Map<String,UserEntity> users;

    public static LoginUser loginUser;

    static {
        init();
    }

    /**
     * 初始化数据
     */
    private static void init() {
        books = new ArrayList<>();
        //初始化5的容量
        users = new HashMap<>(5);
        //定义小数格式化
        NumberFormat format = NumberFormat.getInstance();
        //设置小数位数为2
        format.setMaximumFractionDigits(2);
        for (int i = 0; i < 10; i++) {
            //将随机生成的double类型转为两位小数的字符串
            String str = format.format((new Random().nextDouble() * 100));
            //再通过强转转换回来
            Double price = Double.parseDouble(str);
            books.add(new BookEntity(i + 1, "Eddie", price, "Eddie", new Date(), 0));
        }
    }
}
